package com.xlblog.blog.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xlblog.blog.entity.TBlog;
import com.xlblog.blog.entity.TComment;
import com.xlblog.blog.entity.TUser;
import com.xlblog.blog.service.TBlogService;
import com.xlblog.blog.service.TCommentService;
import com.xlblog.blog.utils.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjy
 * @since 2021-8-18
 */
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class TCommentController {

    @Autowired
    private TCommentService commentService;

    @Autowired
    private TBlogService tBlogService;

    //查询评论列表
    @GetMapping("/comments/{blogId}")
    public List<TComment> comments(@PathVariable Long blogId) {
        return commentService.listCommentByBlogId(blogId);
    }

    //查询所有评论
    @GetMapping("/getCommentByPage")
    public Page<TComment> getCommentByPage(Long current, Long size){
        return commentService.getCommentByPage(current, size);
    }

    //根据评论人姓名查询评论
    @GetMapping("/comments")
    public List<TComment> findCommentByNickname(String nickname){
        return commentService.findCommentByNickname(nickname);
    }


    //新增评论
    @PostMapping("/comments")
    public List<TComment> post(@RequestBody TComment tComment,Authentication authentication,HttpServletRequest request) {
        //获取评论人的ip
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        Long blogId = tComment.getBlogId();
        System.out.println(tComment.getAdminComment());
        if(tComment.getAdminComment() != null){
//        if (authentication != null){
            //设置用户登录
            //TUser user = (TUser)authentication.getPrincipal();
            //设置头像
            //tComment.setAvatar(user.getAvatar());

            //设置头像
            tComment.setAvatar("http://r.photo.store.qq.com/psc?/V518IhTc1ak1tt3yl2dJ2vkXwd4UwPld/45NBuzDIW489QBoVep5mcfMu*HmmR0w5v7KAt.3ECPrwZwtnN9caZpYGizKb*7wFdm3Q86TlYHiXGHwWhTFbp6hdFFLgyclBGPyj4X5QKOY!/r");
            //设置为管理员登录状态
            tComment.setAdminComment(true);
            //设置ip
            tComment.setIp(ip);
        }
        else {
            //设置默认头像
            tComment.setAvatar("http://r.photo.store.qq.com/psc?/V518IhTc1ak1tt3yl2dJ2vkXwd4UwPld/45NBuzDIW489QBoVep5mcU.aRkJViRT6pD58Ru81YZnOvZEo6BS7kgK0laTBqqSt9vussHp1xawpN9RoezNMmzwOCWyisW17SELKf89mktw!/r");
            //设置ip
            tComment.setIp(ip);
        }
        if (tComment.getParentComment() != null) { //为某个评论的子评论
            //
            tComment.setParentCommentId(tComment.getParentComment().getId());
        }
        commentService.saveComment(tComment); //插入数据库

        // 设置每条博客的评论数
        TBlog tBlog = tBlogService.getById(blogId); //获取博客id
        tBlog.setCommentCount(tBlog.getCommentCount() + 1); //设置评论数
        tBlogService.updateByComment(tBlog); //改变表

        return commentService.listCommentByBlogId(blogId); //返回数据列表
    }

//    //新增评论
//    @PostMapping("/comments")
//    public RespBean post(@RequestBody HashMap<String,Object> params, Authentication authentication, HttpServletRequest request) {
//        System.out.println(params);
//        //获取评论人的ip
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_CLIENT_IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//
//        return commentService.saveComment(params,ip);
//    }

    //删除评论
    @DeleteMapping("/deleteComment")
    public String delete(Long id){
        commentService.deleteComment(id);
        return "OK";
    }

    // 获取总评论数
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/getCommentCount")
    public int getCommentCount(){
        return commentService.getCommentCount();
    }

//    //管理员登录
//    @GetMapping("/login")
//    public List<TComment> login(@RequestBody String username,String password) {
//        return commentService.login(username,password);
//    }

}
